package com.quad.innovators.salesease.context;

import com.quad.innovators.salesease.model.dto.SysUserAuthInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class UserContext implements ApplicationContextAware {

    private static ApplicationContext context;
    private static final ThreadLocal<SysUserAuthInfo> currentUser = new ThreadLocal<>();

    public static void setContext(ApplicationContext applicationContext) {
        UserContext.context = applicationContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        UserContext.setContext(applicationContext);
    }

    public static void setCurrentUser(SysUserAuthInfo sysUserAuthInfo) {
        currentUser.set(sysUserAuthInfo);
    }

    public static SysUserAuthInfo getCurrentUser() {
        return currentUser.get();
    }

    public static <T> T getBean(Class<T> beanClass) {
        return context.getBean(beanClass);
    }
}